package cn.dwj.mongo3.demo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/12/12 0012.
 */
public class MongoDemo {
    public static void main(String[] args) {
        MongoClientURI uri = new MongoClientURI("mongodb://ding:ding@192.168.72.146:8088/test");
        MongoClient client = new MongoClient(uri);
        MongoDatabase db =  client.getDatabase("student");
        System.out.println("count collection: " + db.getCollection("student_collection").count());

        MongoCollection col = db.getCollection("student_collection");
        /*List<Document> docs = new ArrayList<Document>();
        for(int x = 0; x < 23; x++){
            Document doc = new Document();
            doc.append("sid", x);
            doc.append("sname", "wei_"+x);
            doc.append("adds", "GUANGDONG_"+x);
            docs.add(doc);
        }
        col.insertMany(docs);*/

        //查询
       /* MongoCursor<Document> cursor = col.find().iterator();
        while (cursor.hasNext()){
            //System.out.println(cursor.next());
            Document doc = cursor.next();
            System.out.println(doc.get("sid")+","+doc.get("sname")+","+doc.get("adds"));;
        }*/

       //条件查询
        /*BasicDBObject cond1 = new BasicDBObject();
        cond1.put("sid", new BasicDBObject("$gt", 5).append("$lt", 10));
        MongoCursor<Document> cursor = col.find(cond1).iterator();
        while (cursor.hasNext()){
            //System.out.println(cursor.next());
            Document doc = cursor.next();
            System.out.println(doc.get("sid")+","+doc.get("sname")+","+doc.get("adds"));;
        }*/

        //模糊查询
        /*BasicDBObject cond1 = new BasicDBObject();
        Pattern pattern = Pattern.compile("4");
        cond1.append("sname", new BasicDBObject("$regex", pattern));
        MongoCursor<Document> cursor = col.find(cond1).iterator();
        while (cursor.hasNext()){
            //System.out.println(cursor.next());
            Document doc = cursor.next();
            System.out.println(doc.get("sname")+","+doc.get("sname")+","+doc.get("adds"));;
        }*/

        //分页显示
        /*MongoCursor<Document> cursor = col.find().skip(1).limit(5).iterator();
        while (cursor.hasNext()){
            //System.out.println(cursor.next());
            Document doc = cursor.next();
            System.out.println(doc.get("sid")+","+doc.get("sname")+","+doc.get("adds"));;
        }*/

        //更新
        /*BasicDBObject condA = new BasicDBObject("sid", 0);//查询条件
        BasicDBObject condB = new BasicDBObject("$set", new BasicDBObject("sname", "super man"));
        UpdateResult result = col.updateMany(condA, condB);
        System.out.println(result);*/

        //删除
        /*BasicDBObject condA = new BasicDBObject("sid", 0);
        DeleteResult result = col.deleteOne(condA);
        System.out.println(result.getDeletedCount());*/

        //统计查询

        client.close();
    }
}
